﻿Imports System.Data.OleDb
Public Class frmCreateAccount

#Region "NotifyIcon"

    Private Sub frmCreateAccount_Resize(sender As Object, e As EventArgs) Handles Me.Resize
        Try
            If Me.WindowState = FormWindowState.Minimized Then
                Me.Visible = False
                NotifyIcon.Visible = True
                NotifyIcon.ShowBalloonTip(1, "SkyCoffee", "SkyCoffee is Still Running!", ToolTipIcon.Info)
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub NotifyIcon_MouseDoubleClick(sender As Object, e As MouseEventArgs) Handles NotifyIcon.MouseDoubleClick
        Try
            Me.Visible = True
            Me.WindowState = FormWindowState.Normal
            NotifyIcon.Visible = False
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub mnuLaunchApplication_Click(sender As Object, e As EventArgs) Handles mnuLaunchApplication.Click
        Try
            Me.Visible = True
            Me.WindowState = FormWindowState.Normal
            NotifyIcon.Visible = False
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub mnuExitApplication_Click(sender As Object, e As EventArgs) Handles mnuExitApplication.Click
        Application.Exit()
    End Sub
#End Region

    Dim ds As New DataSet()
    Dim intCurrentIndex As Integer = 0
    Dim da As New OleDbDataAdapter()
    Dim conn As New OleDbConnection()

    Private Sub frmCreateAccount_Load(sender As Object, e As EventArgs) Handles Me.Load
        'Loads frmAccountMenu
        pnlIcons.Controls.Clear()
        pnlIcons.Controls.Add(frmAccountMenu.pnlIcons)

        'Load ToolTips
        AccountToolTips()

        'Add to database
        conn.ConnectionString = "Provider=Microsoft.jet.oledb.4.0;" & _
                "Data Source=|DataDirectory|\SkyCoffee.mdb"

        da.SelectCommand = New OleDbCommand("SELECT * FROM tblAccounts")
        da.SelectCommand.Connection = conn

        da.InsertCommand = New OleDbCommand("INSERT INTO tblAccounts(fldStaffNumber, fldStaffUsername, fldStaffPassword, fldAccountType) VALUES(@fldStaffNumber,@fldStaffUsername,@fldStaffPassword,@fldAccountType)")
        da.InsertCommand.Connection = conn
        da.InsertCommand.Parameters.Add("@fldStaffNumber", OleDbType.Numeric, 6, "fldStaffNumber")
        da.InsertCommand.Parameters.Add("@fldStaffUsername", OleDbType.VarChar, 40, "fldStaffUsername")
        da.InsertCommand.Parameters.Add("@fldStaffPassword", OleDbType.VarChar, 40, "fldStaffPassword")
        da.InsertCommand.Parameters.Add("@fldAccountType", OleDbType.VarChar, 40, "fldAccountType")

        da.Fill(ds)

        'Load tblAccountType into ComboBox
        Dim connString As String
        Dim connAccountType As New System.Data.OleDb.OleDbConnection()
        Dim daAccountType As New System.Data.OleDb.OleDbDataAdapter()
        Dim dsAccountType As DataSet

        connString = "Provider=Microsoft.jet.oledb.4.0;" & _
            "Data Source=|DataDirectory|\SkyCoffee.mdb"

        connAccountType = New System.Data.OleDb.OleDbConnection(connString)
        connAccountType.Open()
        daAccountType = New System.Data.OleDb.OleDbDataAdapter("SELECT * FROM tblAccountType", connAccountType)
        dsAccountType = New DataSet("Combo")
        daAccountType.Fill(dsAccountType, "Combo")
        cmbAccountType.DataSource = dsAccountType.Tables("Combo")

        cmbAccountType.LoadData() 'Load the ComboBox functions
    End Sub
    Public Sub AddNewRecord()
        'Reset form fields
        cmbAccountType.Value = ""
        txtStaffID.Text = ""
        txtUsername.Text = ""
        txtPassword.Text = ""

        cmbAccountType.Focus()
    End Sub

    Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
        'Validate that all fields have been filled in correctly
        If IsNumeric(txtStaffID.Text) = False Or txtStaffID.Text.Length > 4 Then
            MsgBox("Please enter a valid Staff ID")
            txtStaffID.Focus()
        ElseIf cmbAccountType.Text = "" Or txtUsername.Text = "" Or txtPassword.Text = "" Then
            MsgBox("Please fill in all of the fields")
        Else
            'Database Operations: Add all data to Access Database
            Dim dr As DataRow

            dr = ds.Tables(0).NewRow()  'Gets a reference to a new row.
            dr("fldStaffNumber") = txtStaffID.Text

            dr("fldStaffUsername") = txtUsername.Text
            dr("fldStaffPassword") = txtPassword.Text
            dr("fldAccountType") = cmbAccountType.Value

            ds.Tables(0).Rows.Add(dr)
            da.Update(ds)
            ds.AcceptChanges()

            AddNewRecord()

            MsgBox("Account created successfully")

        End If
    End Sub

    Private Sub btnCancel_Click(sender As Object, e As EventArgs) Handles btnCancel.Click
        AddNewRecord()
    End Sub
End Class